##
## This script generates text necessary to:
##      (1) generate "calculated" metrics in the Poll file
##      (2) generate drawing modes for DModeManager
##


def genCalculatedMetrics()
    f = File.new("/Users/macuser/Desktop/2010_Election_Final_Results.csv")
    $lines = f.readlines;
    f.close

    $nameArray = Array.new;
    $origNameArray = Array.new;
    
    $lines.each_index { |i| 
        if(i == 0)
            next
        else
            splitline = $lines[i].split(',')
            name = splitline[10].strip
            if ((name != "\"Over Votes\"") && (name != "\"Under Votes\""))
                $nameArray.push(name.upcase)
                $origNameArray.push(name)
            end
        end
        
    }

    $nameArray.uniq!
    $origNameArray.uniq!

    out = String.new
    lineArray = Array.new
    initialArray = Array.new
    lastNameArray = Array.new
    $nameOutArray = Array.new
    
    $raceArray = Array.new
    $wardArray = Array.new

    ## extract each name's race type and ward (if applicable) from $lines array 
    $nameArray.each{ |n|
        $lines.each{ |l|
            splitline = l.split(",")
            if(splitline[10].upcase == n)
                $raceArray.push(splitline[2])
                $wardArray.push(splitline[4])
                break
            end
        } 
    }
    
    $nameArray.each_index { |i|
        initialArray.clear
        lineArray = $nameArray[i].split(' ')
        lastName = lineArray[-1].split("\"")[0].downcase
        ## check for hyphen names
        lastNameArray = lastName.split("-")
        if(lastNameArray.length > 1)
            lastNameArray[1] = lastNameArray[1][0,1].upcase + lastNameArray[1][1..-1]
            lastName = lastNameArray.join
        end
        ## check for duplicate names; create alternates in the case of duplicates
        lastName = checkDuplicateNames($nameOutArray, lastName)
        $nameOutArray.push(lastName)
        
        case($raceArray[i][1..-2])
        when "Mayor"
            race = "Mayor"
        when "Councillor"
            race = "Council"
        when "Catholic School Board Trustee"
            race = "CSB"
        when "Public School Board Trustee"
            race = "PSB"
        end
        
        ## write output    
        out += "    float #{lastName}Votes = (metricHash.get(#{$nameArray[i]}));\n"
        out += "    float #{lastName}Percent = (#{lastName}Votes == -1) ? -1 : #{lastName}Votes / total#{race}Votes * 100;\n"
        out += "    metricHash.put(\"#{lastName}Percent\", #{lastName}Percent);\n\n"
    }

    fo = File.new("cmOut.txt", 'w')
    fo.write(out)
    fo.close()
end

def checkDuplicateNames(nameArray, name)
    
    ## check if name is already used
    if(nameArray.include?(name))
        
        ## create alternate name. Check first if name is an 
        ## alternate name itself
        if(name.split("_").length > 1)
            altName = name.split("_")[0] + (name.split("_")[1].to_i + 1).to_s
        else
            altName = name + "_1"
        end
        
        ## check alternate name
        name = checkDuplicateNames(nameArray, altName)
        
    end
    
    ## return altName
    return name
    
end

def genDModes()

   out = String.new

   ## write mayor races
   out += "    // mayoral voting percentages\n"
   out += "\n"
   out += "    DModeGroup mayorRacesDMG = new DModeGroup(\"Mayor\");\n\n"
   
   $raceArray.each_index{ |i|
       if($raceArray[i] == "\"Mayor\"")
           lastName = $nameArray[i].split(" ")[-1][0,1] + $nameArray[i].split(" ")[-1][1..-2].downcase
           ##lastName = $origNameArray[i]
           out+= "    dModes.add(new DMode(\"Percentage #{lastName}\", polls, ColourTable.PURPLES, \"#{$nameOutArray[i]}Percent\", true, 0, 100, maxMode));\n"
           out+= "    mayorRacesDMG.addChild(new DModeGroup((dModes.get(dModes.size() - 1)), dModes.size() - 1));\n"
       end 
   }
        
   ## write council races
   out += "\n    // council and schoolboard voting percentages\n"
   out += "\n"
   out += "    DModeGroup councilRacesDMG = new DModeGroup(\"Council\");\n"
   out += "    DModeGroup psbRacesDMG = new DModeGroup(\"Public School Board\");\n"
   out += "    DModeGroup csbRacesDMG = new DModeGroup(\"Catholic School Board\");\n"
   out += "\n"
   
   wardName = String.new
   
   $raceArray.each_index{ |i|
       if($raceArray[i] == "\"Councillor\"" && $wardArray[i][1..-2] != wardName)
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    DModeGroup #{wardNameShort}DMG = new DModeGroup(#{$wardArray[i]});\n"
       elsif($raceArray[i] == "\"Public School Board Trustee\"" && $wardArray[i][1..-2] != wardName) 
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    DModeGroup #{wardNameShort}DMG = new DModeGroup(#{$wardArray[i]});\n"
       elsif($raceArray[i] == "\"Catholic School Board Trustee\"" && $wardArray[i][1..-2] != wardName) 
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    DModeGroup #{wardNameShort}DMG = new DModeGroup(#{$wardArray[i]});\n"
       end
   }
   
   out += "\n"
   
   $raceArray.each_index{ |i|
       if($raceArray[i] == "\"Councillor\"" || $raceArray[i] == "\"Catholic School Board Trustee\"" || $raceArray[i] == "\"Public School Board Trustee\"" )
           lastName = $nameArray[i].split(" ")[-1][0,1] + $nameArray[i].split(" ")[-1][1..-2].downcase
           wardName = $wardArray[i].split(" ")[0].downcase + $wardArray[i].split(" ")[1]
           wardName = wardName[1..-2]
           if($raceArray[i] == "\"Councillor\"")
               colour = "REDS"
           elsif($raceArray[i] == "\"Catholic School Board Trustee\"")
               colour = "GREENS"
           elsif($raceArray[i] == "\"Public School Board Trustee\"")
               colour = "BLUES"
           end
           out += "    dModes.add(new DMode(\"Percentage #{lastName}\", polls, ColourTable.#{colour}, \"#{$nameOutArray[i]}Percent\", true, 0, 100, maxMode));\n"
           out += "    #{wardName}DMG.addChild(new DModeGroup((dModes.get(dModes.size() - 1)), dModes.size() -1));\n"
       end 
   }
   
   out += "\n"
   
   $raceArray.each_index{ |i|
       if($raceArray[i] == "\"Councillor\"" && $wardArray[i][1..-2] != wardName)
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    councilRacesDMG.addChild(#{wardNameShort}DMG);\n"
       elsif($raceArray[i] == "\"Public School Board Trustee\"" && $wardArray[i][1..-2] != wardName) 
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    psbRacesDMG.addChild(#{wardNameShort}DMG);\n"
       elsif($raceArray[i] == "\"Catholic School Board Trustee\"" && $wardArray[i][1..-2] != wardName) 
           wardName = $wardArray[i][1..-2]
           wardNameShort = wardName.split(" ")[0].downcase + wardName.split(" ")[1]
           out += "    csbRacesDMG.addChild(#{wardNameShort}DMG);\n"
       end
   }
   
   out += "\n"
   
   out += "    masterDMG.addChild(mayorRacesDMG);\n"
   out += "    masterDMG.addChild(councilRacesDMG);\n"
   out += "    masterDMG.addChild(psbRacesDMG);\n"
   out += "    masterDMG.addChild(csbRacesDMG);\n"
   
   fo2 = File.new("dmOut.txt", 'w')
   fo2.write(out)
   fo2.close()
   
end

genCalculatedMetrics
genDModes